const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: "yes",
    checked: false,
    tabList: [{
      name: "已上架",
      value: "yes"
    },{
      name: "待审核",
      value: "wait"
    },{
      name: "审核驳回",
      value: "close"
    }],
    dataList: [],
    finished: false,
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDataList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getDataList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChange(ev) {
    let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.enterprise.need-hide-switch");
    app._postNetWork({
      url: baseUrl,
      data: { 
        is_hide: ev.currentTarget.dataset.item.is_hide == 1 ? 0 : 1 , 
        id: ev.currentTarget.dataset.item.id 
      },
      success: ({data : { data,result,msg }}) => {
        app.tips(msg)
        if(result == 1) {
          this.setData({
            ["dataList[" +ev.currentTarget.dataset.index + "].is_hide"] : ev.currentTarget.dataset.item.is_hide == 1 ? 0 : 1
          })
        }
      }
    });
    // this.setData({ checked: ev.detail });
  },
  catchTap() {},
  goDemandPage(ev) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/enterprise_application/demand_publish/index?type=check&id=${ev.currentTarget.dataset.id}`,
    });
  },
  checkOrder(ev) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/order/order_detail/index?type=enterprise&order_id=${ev.currentTarget.dataset.order_id}`,
    });
  },
  clickTab(ev) {
    this.setData({
      state: ev.detail.name,
      dataList: [],
      finished: false,
      page: 1,
    })
    this.getDataList();
  },
  getDataList () {
    if (this.data.finished) return;
    let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.enterprise.need");
    app._postNetWork({
      url: baseUrl,
      data: {
        state:this.data.state, 
        page: this.data.page
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          app.tips(respone.data.msg)
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let dataList = [];
        if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
          finished = true;
        }
        dataList = this.data.dataList.concat(res.data);

        this.setData({
          finished,
          dataList,
          page: ++this.data.page
        });

      }
    });
  },
})